package com.ssy.lingxi.settle.accounts.serviceimpl;

import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.settle.accounts.entity.StatementCirculationRecordDo;
import com.ssy.lingxi.settle.accounts.model.constant.ReconciliationStatusEnum;
import com.ssy.lingxi.settle.accounts.model.constant.SettleAccountsServiceContants;
import com.ssy.lingxi.settle.accounts.model.vo.response.StatementCirculationRecordVO;
import com.ssy.lingxi.settle.accounts.repository.StatementCirculationRecordRepository;
import com.ssy.lingxi.settle.accounts.service.IStatementCirculationRecordService;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对账单流转记录接口实现
 * @author Saske
 * @date 2021/12/2
 * @version 2.0.0
 */
@Service
public class IStatementCirculationRecordServiceImpl implements IStatementCirculationRecordService {

    @Resource
    private StatementCirculationRecordRepository recordRepository;


    /**
     * 新增对账单流转记录
     * @author Saske
     * @date 2021/12/2
     * @param user : 新增对账单流转记录
     * @param reconciliationNo : 对账单编号
     * @param status : 状态
     * @param operation : 操作
     * @param remark : 意见
     **/
    @Override
    public void addRecord(UserLoginCacheDTO user, String reconciliationNo, Integer status, String operation, String remark) {

        StatementCirculationRecordDo recordDo = new StatementCirculationRecordDo();
        recordDo.setReconciliationNo(reconciliationNo);
        recordDo.setOperationRole(user.getMemberRoleName());
        recordDo.setStatus(status);
        recordDo.setOperation(operation);
        recordDo.setOperationTime(LocalDateTime.now());
        recordDo.setRemark(remark);

        recordRepository.saveAndFlush(recordDo);
        Wrapper.success();
    }

    /**
     * 业务对账单明细行信息
     * @param reconciliationNo 对账单编号
     * @return 对账单明细行信息
     */
    @Override
    public List<StatementCirculationRecordVO> getRecordList(String reconciliationNo) {

        Specification<StatementCirculationRecordDo> spec = (root, query, builder) -> {
            Predicate field1 = builder.equal(root.get("reconciliationNo").as(String.class), reconciliationNo);
            Order orderField1 = builder.asc(root.get("operationTime").as(LocalDateTime.class));
            return query.where(field1).orderBy(orderField1).getRestriction();
        };
        List<StatementCirculationRecordDo> recordDos = recordRepository.findAll(spec);

        return recordDos.stream().map(record -> {
            StatementCirculationRecordVO recordVO = new StatementCirculationRecordVO();
            recordVO.setOperationRole(record.getOperationRole());
            recordVO.setStatus(ReconciliationStatusEnum.getNameByCode(record.getStatus()));
            recordVO.setOperation(record.getOperation());
            recordVO.setOperationTime(record.getOperationTime().format(SettleAccountsServiceContants.DEFAULT_TIME_FORMATTER));
            recordVO.setRemark(record.getRemark());
            return recordVO;
        }).collect(Collectors.toList());
    }
}
